﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using NmbpProject2.BusinessLayer;
using NmbpProject2.DboClasses;

namespace NmbpProject2.PresentationLayer
{
    public partial class PeopleForm : Form
    {
        public PeopleForm()
        {
            InitializeComponent();
            refreshDataSource();
        }

        /// <summary>
        /// Dohvaća popis svih ljudi. Listu svih ljudi povezuje
        /// kao izvor podataka za dataGridView na formi.
        /// </summary>
        private void refreshDataSource()
        {
            PersonBL pBL = new PersonBL();
            List<Person> allPeople = pBL.getAllPeople();
            dataGridViewPeople.DataSource = allPeople;
            dataGridViewPeople.Refresh();
        }

        /// <summary>
        /// Metoda koja se aktivira klikom na gumb za dodavanje nove
        /// osobe.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonAddPerson_Click(object sender, EventArgs e)
        {
            if (validateInput(txtAddFirstName.Text, txtAddLastName.Text, txtAddJmbg.Text))
            {
                PersonBL pBL = new PersonBL();
                try
                {
                    pBL.insertPerson(txtAddFirstName.Text, txtAddLastName.Text, long.Parse(txtAddJmbg.Text));
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message);
                }
                refreshDataSource();
                txtAddFirstName.Text = "";
                txtAddLastName.Text = "";
                txtAddJmbg.Text = "";
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonEditPerson_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection selectedRows = dataGridViewPeople.SelectedRows;
            if (selectedRows.Count == 1)
            {
                Person person = (Person) selectedRows[0].DataBoundItem;
                if (validateInput(txtEditFirstName.Text, txtEditLastName.Text, txtEditJmbg.Text))
                {
                    PersonBL pBL = new PersonBL();
                    try
                    {
                        pBL.update(person, txtEditFirstName.Text, txtEditLastName.Text, long.Parse(txtEditJmbg.Text));
                    }
                    catch (Exception exception)
                    {
                        MessageBox.Show(exception.Message);
                    }
                    refreshDataSource();
                }
            }
        }

        /// <summary>
        /// Metoda koja se aktivira klikom na gumb za brisanje označene
        /// osobe ili osoba.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonDeletePerson_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection selectedRows = dataGridViewPeople.SelectedRows;
            if (selectedRows.Count > 0)
            {
                List<Person> people = new List<Person>();
                PersonBL pBL = new PersonBL();
                for (int i = 0; i < selectedRows.Count; i++)
                {
                    // people.Add (pBL.findByJmbg ((long) selectedRows[i].Cells[2].Value));
                    people.Add((Person) selectedRows[i].DataBoundItem);
                }
                if (MessageBox.Show(
                        "Are you sure you want to delete selected people?",
                        "Confirm delete",
                        MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    pBL.delete(people);
                }
                refreshDataSource();
            }
        }

        /// <summary>
        /// Metoda koja popuni text polja za uređivanje podataka s podacima 
        /// o trenutno označenoj osobi.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridViewPeople_SelectionChanged(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection selectedRows = dataGridViewPeople.SelectedRows;
            if (selectedRows.Count == 1)
            {
                Person person = (Person) selectedRows[0].DataBoundItem;
                txtEditFirstName.Text = person.FirstName;
                txtEditLastName.Text = person.LastName;
                txtEditJmbg.Text = person.Jmbg.ToString();
            }
            else
            {
                txtEditFirstName.Text = "";
                txtEditLastName.Text = "";
                txtEditJmbg.Text = "";
            }
        }

        private bool validateInput(string firstName, string lastName, string jmbgString)
        {
            bool valid = true;
            try
            {
                long jmbg = long.Parse(jmbgString);
                if (firstName.Length == 0 || lastName.Length == 0)
                {
                    throw new Exception("First and last name cannot be empty!");
                }
                if (jmbg.ToString().Length != 13)
                {
                    throw new Exception("JMBG must have 13 digits!");
                }
            }
            catch (FormatException)
            {
                valid = false;
                MessageBox.Show("JMBG must contain only numeric digits!");
            }
            catch (Exception exception)
            {
                valid = false;
                MessageBox.Show(exception.Message);
            }
            return valid;
        }

        private void PeopleForm_Load(object sender, EventArgs e)
        {
        }
    }
}